package com.longagoewk.springboot.dao;

import com.longagoewk.springboot.LogConfig;
import com.longagoewk.springboot.entity.MuseumBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class MuseumDao {
    //JDBC访问数据模板
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //获取博物馆实体
    public MuseumBean getMuseum(MuseumBean museumBean)
    {
        String sql = "SELECT * FROM museum where mname=? or mID=?";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(MuseumBean.class),museumBean.getMname(),museumBean.getmID());
        }catch (EmptyResultDataAccessException e)
        {
            LogConfig.warning("数据库 - 未存在该博物馆信息:"+museumBean.getMname());
            return null;
        }
    }

    //更新博物馆信息
    public boolean updateUser(MuseumBean museumBean){
        String sql = "UPDATE museum SET mname = ?, mintro = ?, mdetailed = ?, mimage = ? WHERE mID = ?";
        try {
            int result = jdbcTemplate.update(sql,museumBean.getMname(),museumBean.getMintro(),museumBean.getMdetailed(),museumBean.getMimage(),museumBean.getmID());
            return result > 0;
        } catch (DataAccessException e) {
            LogConfig.error("数据库 - "+museumBean.getmID()+"更新博物馆信息失败");
            return false;
        }
    }
}
